package com.demo.goods.controller;

import com.demo.common.entity.Goods;
import com.demo.common.response.Result;
import com.demo.goods.service.GoodsService;
import com.order.feign.client.OrderServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;

/**
 * 商品控制器
 * 提供商品的增删改查接口
 *
 * @author demo
 * @since 1.0.0
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin(origins = "*", maxAge = 3600) // 允许跨域，生产环境应该限制具体域名
@Validated
@Slf4j
public class GoodsController {

    private final GoodsService goodsService;
    private final OrderServiceClient orderServiceClient;

    /**
     * 构造函数注入依赖
     * 
     * @param goodsService 商品服务
     * @param orderServiceClient 订单服务客户端
     */
    @Autowired
    public GoodsController(GoodsService goodsService, OrderServiceClient orderServiceClient) {
        this.goodsService = goodsService;
        this.orderServiceClient = orderServiceClient;
    }

    /**
     * 获取所有商品列表
     * 
     * @return 商品列表
     */
    @GetMapping
    public Result<?> getAll() {
        try {
            log.info("开始获取所有商品列表");
            return Result.success(goodsService.list());
        } catch (Exception e) {
            log.error("获取商品列表失败: {}", e.getMessage(), e);
            return Result.error("获取商品列表失败");
        }
    }

    /**
     * 根据ID获取单个商品信息
     * 
     * @param id 商品ID
     * @return 商品信息
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable("id") @NotNull @Positive Long id) {
        try {
            log.info("开始获取商品信息，商品ID: {}", id);
            Goods goods = goodsService.getById(id);
            if (goods == null) {
                log.warn("商品不存在，商品ID: {}", id);
                return Result.error("商品不存在");
            }
            return Result.success(goods);
        } catch (Exception e) {
            log.error("获取商品信息失败，商品ID: {}, 错误信息: {}", id, e.getMessage(), e);
            return Result.error("获取商品信息失败");
        }
    }

    /**
     * 新增商品
     * 
     * @param goods 商品信息
     * @return 操作结果
     */
    @PostMapping
    public Result<?> add(@RequestBody @Valid Goods goods) {
        try {
            log.info("开始新增商品: {}", goods.getName());
            boolean success = goodsService.save(goods);
            if (success) {
                log.info("商品新增成功，商品ID: {}", goods.getId());
                return Result.success(goods);
            } else {
                log.warn("商品新增失败: {}", goods.getName());
                return Result.error("商品新增失败");
            }
        } catch (Exception e) {
            log.error("商品新增失败: {}, 错误信息: {}", goods.getName(), e.getMessage(), e);
            return Result.error("商品新增失败");
        }
    }

    /**
     * 更新商品信息
     * 
     * @param goods 商品信息
     * @return 操作结果
     */
    @PutMapping
    public Result<?> update(@RequestBody @Valid Goods goods) {
        try {
            log.info("开始更新商品信息，商品ID: {}", goods.getId());
            boolean success = goodsService.updateById(goods);
            if (success) {
                log.info("商品更新成功，商品ID: {}", goods.getId());
                return Result.success("商品更新成功");
            } else {
                log.warn("商品更新失败，商品ID: {}", goods.getId());
                return Result.error("商品更新失败");
            }
        } catch (Exception e) {
            log.error("商品更新失败，商品ID: {}, 错误信息: {}", goods.getId(), e.getMessage(), e);
            return Result.error("商品更新失败");
        }
    }

    /**
     * 删除商品
     * 
     * @param id 商品ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable("id") @NotNull @Positive Long id) {
        try {
            log.info("开始删除商品，商品ID: {}", id);
            boolean success = goodsService.removeById(id);
            if (success) {
                log.info("商品删除成功，商品ID: {}", id);
                return Result.success("商品删除成功");
            } else {
                log.warn("商品删除失败，商品ID: {}", id);
                return Result.error("商品删除失败");
            }
        } catch (Exception e) {
            log.error("商品删除失败，商品ID: {}, 错误信息: {}", id, e.getMessage(), e);
            return Result.error("商品删除失败");
        }
    }
}
